package com.lzy.chat.c_service;

import com.lzy.chat.a_entity.AddGroup;
import com.lzy.chat.a_entity.Group;
import com.lzy.chat.a_entity.GroupShip;
import com.lzy.chat.b_mapper.GroupMapper;
import com.lzy.chat.b_mapper.UserMapper;
import com.lzy.chat.c_service.Exception.Exceptions;
import com.lzy.chat.c_service.Exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GroupServiceImpl implements GroupService {
    private GroupMapper groupMapper;
    private UserMapper userMapper;

    @Autowired
    public void setGroupMapper(GroupMapper groupMapper) {
        this.groupMapper = groupMapper;
    }

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void createGroup(Group group, Integer uid) {
        Integer res = groupMapper.insertGroup(group);
        if (res != 1) throw new ServiceException(Exceptions.GroupInsertException);
        GroupShip groupShip = new GroupShip();
        groupShip.setGid(group.getId());
        groupShip.setUid(uid);
        groupShip.setPermission(2);
        groupShip.setLastReadDate(new Date());
        Integer res2 = groupMapper.insertGroupShip(groupShip);
        if (res2 != 1) throw new ServiceException(Exceptions.GroupInsertException);
    }

    @Override
    public void addGroupShip(Integer uid_add, Integer uid_permit, Integer gid) {
        Group group = groupMapper.getGroupById(gid);
        if (group == null) throw new ServiceException(Exceptions.GroupNotFoundException);
        if (group.getNeedVerify() > 0) {
            GroupShip g = groupMapper.getGroupShip(uid_permit, gid);
            if (g == null || g.getPermission() < 1)
                throw new ServiceException(Exceptions.GroupPermissionException);
        }
        GroupShip ship = groupMapper.getGroupShip(uid_add, gid);
        if (ship != null) throw new ServiceException(Exceptions.GroupDuplicateException);
        GroupShip groupShip = new GroupShip();
        groupShip.setUid(uid_add);
        groupShip.setGid(gid);
        groupShip.setPermission(0);
        groupShip.setLastReadDate(new Date());
        Integer res = groupMapper.insertGroupShip(groupShip);
        if (res != 1) throw new ServiceException(Exceptions.GroupInsertException);
    }

    @Override
    public void dismissGroup(Integer gid, Integer uid) {
        //查看权限够不够
        GroupShip groupShip = groupMapper.getGroupShip(uid, gid);
        if (groupShip.getPermission() < 2) throw new ServiceException(Exceptions.GroupPermissionException);
        //查询群里所有成员,并删除所有群关系
        List<GroupShip> groupShipList = groupMapper.getGroupShipByGid(gid);
        for (GroupShip g : groupShipList) {
            groupMapper.deleteGroupShip(uid, g.getGid());
        }
        Integer res = groupMapper.deleteGroup(gid);
        if (res != 1) throw new ServiceException(Exceptions.GroupDeleteException);
    }

    @Override
    public void deleteGroupShip(Integer gid, Integer uid) {
        Integer res = groupMapper.deleteGroupShip(uid, gid);
        if (res != 1) throw new ServiceException(Exceptions.GroupDeleteException);
    }

    @Override
    public List<Group> getGroupByName(String keyword) {
        return groupMapper.getGroupByName(keyword);
    }

    @Override
    public Group getGroupByGid(Integer gid) {
        return groupMapper.getGroupById(gid);
    }

    @Override
    public void updateGroupInfo(Group group) {
        Integer res = groupMapper.updateGroupInfo(group);
        if (res != 1) throw new ServiceException(Exceptions.GroupUpdateException, "群信息更新错误");
    }

    @Override
    public void updateLastReadDate(Integer uid, Integer gid, Date date) {
        Integer res = groupMapper.updateLastReadDate(gid, uid, date);
        if (res != 1) throw new ServiceException(Exceptions.GroupUpdateException, "最后阅读时间更新失败");
    }

    @Override
    public List<GroupShip> getGroupShipByGid(Integer gid) {
        return groupMapper.getGroupShipByGid(gid);
    }

    @Override
    public List<GroupShip> getGroupShipByUid(Integer uid) {
        return groupMapper.getGroupShipByUid(uid);
    }

    @Override
    public GroupShip getGroupShipByUidAndGid(Integer uid, Integer gid) {
        return groupMapper.getGroupShip(uid, gid);
    }

    @Override
    public void requestToAddGroup(AddGroup addGroup) {
        //重复申请
        AddGroup addGroup1 = groupMapper.getByUidAndGid(addGroup.getUid(), addGroup.getGid());
        if (addGroup1 != null) throw new ServiceException(Exceptions.AddGroupDuplicateException);
        //重复加入群
        GroupShip groupShip = groupMapper.getGroupShip(addGroup.getUid(), addGroup.getGid());
        if (groupShip != null) throw new ServiceException(Exceptions.GroupDuplicateException);
        addGroup.setIsDeal(0);
        Integer res = groupMapper.insertAddGroup(addGroup);
        if (res != 1) throw new ServiceException(Exceptions.AddGroupInsertException);
    }

    @Override
    public void dealAddGroup(Integer id, Integer deal) {
        Integer res = groupMapper.dealAddGroup(id, deal);
        if (res != 1) throw new ServiceException(Exceptions.AddGroupUpdateException);
    }

    @Override
    public Integer countUnDeal(Integer gid) {
        return groupMapper.countUnDeal(gid);
    }

    @Override
    public List<AddGroup> getUnDealByGid(Integer gid) {
        List<AddGroup> list = groupMapper.getUnDealByGid(gid);
        for (AddGroup addGroup : list) {
            addGroup.setGroup(groupMapper.getGroupById(addGroup.getGid()));
            addGroup.setUser(userMapper.getUserById(addGroup.getUid()));
        }
        return list;
    }

    @Override
    public List<Integer> getOnesManageGroup(Integer uid) {
        return groupMapper.getOnesManageGroup(uid);
    }

    @Override
    public List<Integer> getGroupManager(Integer gid) {
        return groupMapper.getGroupManager(gid);
    }

    @Override
    public List<Integer> getOnesGroup(Integer uid) {
        return groupMapper.getOnesGroup(uid);
    }
}
